<<template>
    <div class="goods-box2">
        <!-- 搜索框 -->
        <div class="search">
            <div style="display: flex;height: 35px;line-height: 35px;justify-content: space-around;">
                <div style="display: flex;height: 35px;line-height: 35px;">
                    <el-input v-model="searchKeys" maxlength="100" clearable style="width:290px;" />
                    <img src="@/assets/img/search.png" @click="handleSearch" class="search-icon" />
                </div>
                <el-button v-show="searchKeys != ''"
                    style="width: 80px;height:35px;line-height: 35px;margin-left: 10px;" @click="backHandle">
                    <el-icon>
                        <DArrowLeft />
                    </el-icon>返 回
                </el-button>
            </div>
            <div style="color:#696969;font-size:12px;margin-top: 10px;">
                热门搜索：
                <a class="tag-item" @click="handleTopicDetail('苹果')">苹果</a>
                <a class="tag-item" @click="handleTopicDetail('新疆哈密瓜')">新疆哈密瓜</a>
                <a class="tag-item" @click="handleTopicDetail('樱桃')">樱桃</a>
            </div>
        </div>

        <!-- 商品列表 -->
        <div class="goods" v-for="(item, index) in cgoods" :key="index" @click="detailsClick(item.orderId)">
            <img :src="item.picture" class="goods-img" />
            <div class="info">
                <p class="content">
                    <i style="color:#696969;font-size:12px;font-style:normal;" v-if="item.type == 'goods'">[供]</i>
                    <i style="color:#696969;font-size:12px;font-style:normal;" v-else>[需]</i>
                    <span class="content-text">{{ item.content }}</span>
                </p>
                <span class="price" v-if="item.price"><i style="font-size:20px">￥</i>{{ item.price }}</span>
                <span class="initiator">{{ item.ownName }} | {{ time(item.createTime) }}</span>
            </div>
        </div>

    </div>
</template>

    <script setup>

    import { defineProps, ref } from 'vue';
    import router from '@/router'
    import { defineEmits } from 'vue';

    const emit = defineEmits(['handleSearch']);

    // 接收父组件传过来的数据
    const props = defineProps({
        cgoods: {
            type: Array,
        }
    })

    //格式化时间
    const time = (value) => {
        let date = new Date(value);
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? "0" + MM : MM;
        let d = date.getDate();
        d = d < 10 ? "0" + d : d;
        let h = date.getHours();
        h = h < 10 ? "0" + h : h;
        let m = date.getMinutes();
        m = m < 10 ? "0" + m : m;
        let s = date.getSeconds();
        s = s < 10 ? "0" + s : s;
        return y + "-" + MM + "-" + d + " " + h + ":" + m;
    }


    //搜索
    const searchKeys = ref('');

    const handleSearch = () => {
        emit('handleSearch', searchKeys.value)
    }

    //关键字搜索
    const handleTopicDetail = (val) => {
        searchKeys.value = val
        handleSearch()
    }

    //返回
    const backHandle = () => {
        searchKeys.value = ''
        //调用父类的方法
        emit('refresh')
    }

    //详情
    const detailsClick = (item) => {
        router.push(`/index/good_details?orderId=${item}`).catch((err) => err);
    }

</script>

    <style lang="scss" scoped>
    .search {
        width: 624px;
        height: 80px;
        background-color: white;
        padding: 10px 20px;

        .tag-item {
            margin-right: 10px;
            cursor: pointer;
            color: blue;
        }

        .search-icon {
            position: relative;
            left: -2px;
            cursor: pointer;
        }
    }

    .goods-box2 {
        background-color: #fff;
        width: 780px;
        margin: 0 auto;

        .goods {
            width: 780px;
            min-height: 120px;
            padding: 10px 20px;
            padding-left: 50px;
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            border-radius: 0px;
            border-bottom: 1px solid #f2f2f2;

            .goods-img {
                width: 100px;
                height: 100px;
                margin-right: 20px;
                border-radius: 6px;
            }

            .info {
                width: 600px;
                text-align: left;

                .initiator {
                    font-size: 15px;
                    color: #666;
                }

                .content {
                    width: 550px;
                    line-height: 20px;
                    /*超出的部分隐藏*/
                    overflow: hidden;
                    /*文字用省略号替代超出的部分*/
                    text-overflow: ellipsis;
                    /*弹性伸缩盒子模型显示*/
                    display: -webkit-box;
                    /*限制在一个块元素显示文本的行数*/
                    -webkit-line-clamp: 7;
                    /*设置或检索伸缩盒对象的子元素排列方式*/
                    -webkit-box-orient: vertical;
                    font-size: 15px;
                    display: flex;
                    flex-direction: row;
                    justify-content: flex-start;

                    .content-text {
                        cursor: pointer;

                        &:hover {
                            color: #035D1C;
                            text-decoration: underline;
                        }
                    }
                }

                .price {
                    width: 100%;
                    font-size: 20px;
                    font-weight: bold;
                    display: block;
                    color: red;
                }
            }
        }
    }

    .search .el-input--suffix .el-input__inner {
        height: 35px;
        line-height: 35px;
    }
</style>